package wodegua.d32_期末考试真题.demo4;

import java.util.List;
import java.util.Map;
import java.util.Random;

public class MyRunnable implements Runnable {
    private final List<Integer> hb;
    private final Map<String, Integer> qhbMap;
    private final Random random = new Random();

    public MyRunnable(List<Integer> hb, Map<String, Integer> qhbMap) {
        this.hb = hb;
        this.qhbMap = qhbMap;
    }

    @Override
    public void run() {
        while (true) {
            synchronized (hb) {
                if (hb.size() == 0) {
                    break;
                }
                int key = random.nextInt(hb.size());
                String name = Thread.currentThread().getName();
                int money = hb.get(key);
                System.out.println(name + "员工抢到" + money + "元");
                qhbMap.put(name, qhbMap.containsKey(name) ? qhbMap.get(name) + money : money);
                hb.remove(key);
                try {
                    Thread.sleep(10);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
